<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px" class="queryForms">
			<el-form-item label="保养级别" prop="maintenanceLevelId">
				<el-select v-model="queryParams.maintenanceLevelId" placeholder="请选择">
					<el-option v-for="dict in dict.type.maintain_level" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="保养项目" prop="maintenanceItem">
				<el-input v-model="queryParams.maintenanceItem" placeholder="请输入保养项目" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:deviceMaintenanceItem:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:deviceMaintenanceItem:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button size="mini" type="info" @click="handleBacks">返回</el-button>
			</el-col>
			<right-toolbar @changeTableData="changeTableData" menuNames="deviceContent14"
:showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="equipment:produce:deviceContent:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="deviceMaintenanceItemList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="保养级别" align="center" prop="maintenanceLevelId" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.maintain_level" :value="scope.row.maintenanceLevelId" />
					</template>
				</el-table-column>
				<el-table-column label="保养项目" prop="maintenanceItem" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" align="center" width="200">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['erp:deviceMaintenanceItem:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:deviceMaintenanceItem:remove']">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination16" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改设备保养项目对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" class="add-form1" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="保养级别" prop="maintenanceLevelId">
					<el-select v-model="form.maintenanceLevelId" placeholder="请选择">
						<el-option v-for="dict in dict.type.maintain_level" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="保养项目" prop="maintenanceItem">
					<el-input v-model="form.maintenanceItem" placeholder="请输入保养项目" maxlength="20" show-word-limit  />
				</el-form-item>
				<el-divider content-position="center">设备保养内容信息</el-divider>
				<div class="maintain-content">
					<div class="maintain-item" v-for="(item, index) of tdeviceMaintenanceContentList" :key="index">
						<el-input type="textarea" v-model="item.maintenanceContent" maxlength="50" show-word-limit ></el-input>
						<el-button type="text" v-if="index == 0" @click="addOne()">创建</el-button>
						<el-button type="text" v-else @click="delOne(index)">删除</el-button>
					</div>
				</div>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import DocumentShow from "@/views/components/DocumentShow"
import { getcolumn } from "@/api/selectList"
import { listDeviceMaintenanceItem, getDeviceMaintenanceItem, delDeviceMaintenanceItem, addDeviceMaintenanceItem, updateDeviceMaintenanceItem } from "@/api/equipment/produce/deviceContent"
import { mapGetters } from "vuex"
export default {
	name: "DeviceMaintenanceItem",
	dicts: ["maintain_level"],
	components: { DocumentShow },
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			fileVedioList: [],
			showfileListDialog: false,
			showTodoListDialog: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 设备id
			deviceId: 0,
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 子表选中数据
			checkedTDeviceMaintenanceContent: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 设备保养项目表格数据
			deviceMaintenanceItemList: [],
			// 设备保养内容表格数据
			tdeviceMaintenanceContentList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				deviceId: undefined,
				maintenanceLevelId: undefined,
				maintenanceItem: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				maintenanceLevelId: [{ required: true, message: "请选择保养级别", trigger: "change" }],
				maintenanceItem: [{ required: true, message: "请输入保养项目", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `保养级别`, visible: true },
				{ key: 1, label: `保养项目`, visible: true }
			]
		}
	},
	created() {
		this.deviceId = this.$route.query.id
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.deviceId = this.$route.query.id
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},

	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination16" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		handleBacks() {
			// 关闭当前tab页签，打开新页签
			const obj = { path: "/equipment/produce/device" }
			this.$tab.closeOpenPage(obj)
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询设备保养项目列表 */
		getList() {
			this.loading = true
			this.queryParams.deviceId = this.deviceId
			listDeviceMaintenanceItem(this.queryParams).then(response => {
				this.deviceMaintenanceItemList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: undefined,
				deviceId: this.deviceId,
				maintenanceLevelId: undefined,
				maintenanceItem: undefined
			}
			this.tdeviceMaintenanceContentList = [{ deviceId: this.form.deviceId }]
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加设备保养项目"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getDeviceMaintenanceItem(id).then(response => {
				this.form = response.data
				this.tdeviceMaintenanceContentList = response.data.tdeviceMaintenanceContentList
				this.open = true
				this.title = "修改设备保养项目"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					let checkone = this.tdeviceMaintenanceContentList.find(item => {
						return !item.maintenanceContent
					})
					if (checkone) {
						this.$modal.msgError("请设置保养内容")
						return false
					}
					this.form.tdeviceMaintenanceContentList = this.tdeviceMaintenanceContentList
					if (this.form.id != undefined) {
						updateDeviceMaintenanceItem(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addDeviceMaintenanceItem(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除数据项？")
				.then(function () {
					return delDeviceMaintenanceItem(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		// 保养内容新增和删除
		addOne() {
			let list = this.tdeviceMaintenanceContentList
			if (!list[list.length - 1].maintenanceContent) {
				this.$modal.msgError("请输入设备保养内容信息")
				return false
			}
			this.tdeviceMaintenanceContentList.push({ deviceId: this.form.deviceId })
		},
		delOne(index) {
			this.tdeviceMaintenanceContentList.splice(index, 1)
		}
		/** 导出按钮操作 */
		// handleExport() {
		//   this.download('erp/deviceMaintenanceItem/export', {
		//     ...this.queryParams
		//   }, `deviceMaintenanceItem_${new Date().getTime()}.xlsx`)
		// }
	}
}
</script>

<style lang="scss" scoped>
.maintain-content {
	padding: 10px 10%;
}
.maintain-item {
	display: flex;
	margin-bottom: 10px;
	justify-content: space-between;
	::v-deep .el-textarea {
		width: 86%;
	}
}
</style>
